/*
 * Presention.java
 *
 * Created on __DATE__, __TIME__
 */

package forms;

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JFrame;

/**
 *
 * @author  __USER__
 */
public class Presention extends javax.swing.JFrame implements Runnable {

    private int current_image;
    private Image[] images;
    private MediaTracker mt;
    private Thread thread;
    private AudioClip b_n;

    //AudioClip b_n;

    /** Creates new form Presention */
    public Presention() {
	initComponents();
    }

    public Presention(int type) {
	initComponents();
	//this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	int counts = -1;
	String name = null;
	switch (type) {
	case 1:
	    counts = 8;
	    name = "image/a";
	    break;
	case 2:
	    counts = 8;
	    name = "image/b";
	    break;
	case 3:
	    counts = 7;
	    name = "image/c";
	    break;
	case 4:
	    counts = 3;
	    name = "image/d";
	    break;
	case 5:
	    counts = 5;
	    name = "image/e";
	    break;
	default:
	    break;
	}
	if (counts > 0) {
	    images = new Image[counts];
	    StringBuilder builder = new StringBuilder(name);
	    mt = new MediaTracker(this);
	    for (int i = 0; i < counts; i++) {
		builder = new StringBuilder(name);
		builder.append(i + 1).append(".jpg");
		Image image = Toolkit.getDefaultToolkit().getImage(
			builder.toString());
		mt.addImage(image, i);
		images[i] = image;
	    }
	    current_image = -1;
	    thread = new Thread(this);
	    try {
		b_n = Applet.newAudioClip(new URL(new File("user.dir").toURL(),
			"audio/bk_nxt.au"));
	    } catch (MalformedURLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    }
	    thread.start();
	}
    }

    //GEN-BEGIN:initComponents
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

	jPanel1 = new javax.swing.JPanel();
	jPanel2 = new javax.swing.JPanel();
	jButton1 = new javax.swing.JButton();
	jButton2 = new javax.swing.JButton();

	setBackground(new java.awt.Color(255, 255, 255));
	setDefaultCloseOperation(3);

	jPanel1.setBackground(new java.awt.Color(255, 255, 255));
	jPanel1.setMaximumSize(new java.awt.Dimension(772, 490));

	javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
		jPanel1);
	jPanel1.setLayout(jPanel1Layout);
	jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(
		javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 768,
		Short.MAX_VALUE));
	jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(
		javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 490,
		Short.MAX_VALUE));

	jPanel2.setBackground(new java.awt.Color(242, 231, 231));

	jButton1.setBackground(java.awt.Color.lightGray);
	jButton1.setText("\u540e\u9000");
	jButton1.addActionListener(new java.awt.event.ActionListener() {
	    public void actionPerformed(java.awt.event.ActionEvent evt) {
		jButton1ActionPerformed(evt);
	    }
	});

	jButton2.setBackground(java.awt.Color.lightGray);
	jButton2.setText("\u524d\u8fdb");
	jButton2.addActionListener(new java.awt.event.ActionListener() {
	    public void actionPerformed(java.awt.event.ActionEvent evt) {
		jButton2ActionPerformed(evt);
	    }
	});

	javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(
		jPanel2);
	jPanel2.setLayout(jPanel2Layout);
	jPanel2Layout
		.setHorizontalGroup(jPanel2Layout
			.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING)
			.addGroup(
				jPanel2Layout
					.createSequentialGroup()
					.addGap(74, 74, 74)
					.addComponent(jButton1)
					.addPreferredGap(
						javax.swing.LayoutStyle.ComponentPlacement.RELATED,
						511, Short.MAX_VALUE)
					.addComponent(jButton2)
					.addGap(69, 69, 69)));
	jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(
		javax.swing.GroupLayout.Alignment.LEADING).addGroup(
		jPanel2Layout
			.createParallelGroup(
				javax.swing.GroupLayout.Alignment.BASELINE)
			.addComponent(jButton1).addComponent(jButton2)));

	javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
		getContentPane());
	getContentPane().setLayout(layout);
	layout.setHorizontalGroup(layout
		.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
		.addComponent(jPanel2,
			javax.swing.GroupLayout.Alignment.TRAILING,
			javax.swing.GroupLayout.DEFAULT_SIZE,
			javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
		.addComponent(jPanel1,
			javax.swing.GroupLayout.Alignment.TRAILING,
			javax.swing.GroupLayout.DEFAULT_SIZE,
			javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
	layout.setVerticalGroup(layout
		.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
		.addGroup(
			javax.swing.GroupLayout.Alignment.TRAILING,
			layout.createSequentialGroup()
				.addComponent(jPanel1,
					javax.swing.GroupLayout.DEFAULT_SIZE,
					javax.swing.GroupLayout.DEFAULT_SIZE,
					Short.MAX_VALUE)
				.addPreferredGap(
					javax.swing.LayoutStyle.ComponentPlacement.RELATED)
				.addComponent(jPanel2,
					javax.swing.GroupLayout.PREFERRED_SIZE,
					javax.swing.GroupLayout.DEFAULT_SIZE,
					javax.swing.GroupLayout.PREFERRED_SIZE)));

	pack();
    }// </editor-fold>
    //GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
	if (current_image < images.length - 1) {
	    synchronized (this) {
		current_image++;
	    }
	    b_n.play();
	    repaint();
	}

    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
	if (current_image > 0) {
	    synchronized (this) {
		current_image--;
	    }
	    b_n.play();
	    // this.invalidate();
	    this.repaint();
	}

    }

    public void paint(Graphics g) {
	super.paint(g);
	if (current_image < images.length) {
	    g = jPanel1.getGraphics();
	    //g.setColor(Color.WHITE);
	    g.drawImage(images[current_image], 1, 1, this);
	    //	    if (mt.checkID(current_image, true)) {
	    //			current_image++;
	    //		}
	}
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
	new Presention(1).setVisible(true);
    }

    //GEN-BEGIN:variables
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;

    // End of variables declaration//GEN-END:variables
    @Override
    public void run() {

	while (current_image < images.length) {
	    synchronized (this) {
		current_image++;
	    }
	    //b_n.play();
	    //this.invalidate();
	    this.repaint();
	    try {
		Thread.sleep(8000);

	    } catch (InterruptedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    }
	    if (current_image >= images.length) {
		current_image = -1;
		//thread.interrupt();
	    }
	}

    }

}